package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.PerformVo;
import com.ruoyi.system.domain.vo.StatisticsVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GxPerformance;
import com.ruoyi.system.service.IGxPerformanceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 绩效Controller
 *
 * @author ruoyi
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/system/performance")
public class GxPerformanceController extends BaseController
{
    @Autowired
    private IGxPerformanceService gxPerformanceService;

    /**
     * 查询绩效列表
     */
    @PreAuthorize("@ss.hasPermi('system:performance:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxPerformance gxPerformance)
    {
        startPage();
        List<GxPerformance> list = gxPerformanceService.selectGxPerformanceList(gxPerformance);
        return getDataTable(list);
    }

    @GetMapping("/statistics")
    public AjaxResult statistics(PerformVo performVo)
    {
        AjaxResult ajax = AjaxResult.success();
        List<StatisticsVo> list = gxPerformanceService.selectGxPerformanceByUserAndDateRange(performVo);
        List<StatisticsVo> dataList = new ArrayList<>();
        for (StatisticsVo sv : list) {
            if (sv.getName().equals("1")) {
                StatisticsVo vo = new StatisticsVo();
                vo.setName("任务创建");
                vo.setValue(sv.getValue());
                dataList.add(vo);
            }
            if (sv.getName().equals("2")) {
                StatisticsVo vo = new StatisticsVo();
                vo.setName("执行评定");
                vo.setValue(sv.getValue());
                dataList.add(vo);
            }
            if (sv.getName().equals("3")) {
                StatisticsVo vo = new StatisticsVo();
                vo.setName("评定审核");
                vo.setValue(sv.getValue());
                dataList.add(vo);
            }
        }
        ajax.put("dataList", dataList);
        return ajax;
    }

    /**
     * 导出绩效列表
     */
    @PreAuthorize("@ss.hasPermi('system:performance:export')")
    @Log(title = "绩效", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxPerformance gxPerformance)
    {
        List<GxPerformance> list = gxPerformanceService.selectGxPerformanceList(gxPerformance);
        ExcelUtil<GxPerformance> util = new ExcelUtil<GxPerformance>(GxPerformance.class);
        util.exportExcel(response, list, "绩效数据");
    }

    /**
     * 获取绩效详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:performance:query')")
    @GetMapping(value = "/{performanceId}")
    public AjaxResult getInfo(@PathVariable("performanceId") Long performanceId)
    {
        return AjaxResult.success(gxPerformanceService.selectGxPerformanceByPerformanceId(performanceId));
    }

    /**
     * 新增绩效
     */
    @PreAuthorize("@ss.hasPermi('system:performance:add')")
    @Log(title = "绩效", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxPerformance gxPerformance)
    {
        return toAjax(gxPerformanceService.insertGxPerformance(gxPerformance));
    }

    /**
     * 修改绩效
     */
    @PreAuthorize("@ss.hasPermi('system:performance:edit')")
    @Log(title = "绩效", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxPerformance gxPerformance)
    {
        return toAjax(gxPerformanceService.updateGxPerformance(gxPerformance));
    }

    /**
     * 删除绩效
     */
    @PreAuthorize("@ss.hasPermi('system:performance:remove')")
    @Log(title = "绩效", businessType = BusinessType.DELETE)
    @DeleteMapping("/{performanceIds}")
    public AjaxResult remove(@PathVariable Long[] performanceIds)
    {
        return toAjax(gxPerformanceService.deleteGxPerformanceByPerformanceIds(performanceIds));
    }
}
